﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PinCity.ORM;
using NHibernate.Criterion;
using NHibernate;
using PinCity.Models.Accounts;

namespace PinCity.Models.Repositories
{
    public class UsersRepository : Repository<User>
    {
        private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(typeof(UsersRepository));

        /// <summary>
        /// Returns a user, given login (username).
        /// </summary>
        /// <param name="login">Username of desired user.</param>
        /// <returns>A user if found or null.</returns>
        public User GetUserByName(string login)
        {
            return this.GetAll(Expression.Eq("Login", login)).FirstOrDefault<User>();
        }

        /// <summary>
        /// Returns a user, given email.
        /// </summary>
        /// <param name="email">Email of desired user.</param>
        /// <returns>A user if found or null.</returns>
        public User GetUserByEmail(string email)
        {
            return this.GetAll(Expression.Eq("Email", email)).FirstOrDefault<User>();
        }

        /// <summary>
        /// Returns set of users with email, that is like to given.
        /// </summary>
        /// <param name="email">Email of desired users.</param>
        /// <returns>Set of found users or null.</returns>
        public IList<User> GetUsersByEmail(string email)
        {
            return this.GetAll(Expression.InsensitiveLike("Email", email));
        }

        /// <summary>
        /// Returns set of users with login, that is like to given.
        /// </summary>
        /// <param name="login">Login of desired users.</param>
        /// <returns>Set of found users, null if none found.</returns>
        public IList<User> GetUsersByName(string login)
        {
            return this.GetAll(Expression.InsensitiveLike("Login", login));
        }
    }
}